package com.zenithmind.chat.pojo.vo;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * AI对话消息VO
 * 遵循单一职责原则，专门用于展示消息信息
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Schema(description = "AI对话消息VO")
public class AiChatMessageVO extends BaseVO {
    
    @Schema(description = "对话ID")
    private String chatId;
    
    @Schema(description = "消息角色(user-用户,assistant-助手,system-系统)")
    private String role;
    
    @Schema(description = "消息内容")
    private String content;
    
    @Schema(description = "父消息ID")
    private String parentId;
    
    @Schema(description = "token数量")
    private Integer tokenCount;
    
    @Schema(description = "状态(0-失败,1-成功,2-处理中)")
    private Integer status;
    
    @Schema(description = "消息类型(text-文本,image-图片,file-文件)")
    private String messageType;
    
    @Schema(description = "附件URL列表")
    private List<String> attachments;
    
    @Schema(description = "消息元数据")
    private String metadata;
    
    @Schema(description = "错误信息")
    private String errorMessage;
    
    @Schema(description = "处理耗时(毫秒)")
    private Long processingTime;
    
    @Override
    public String getDisplayName() {
        if (content == null) {
            return "空消息";
        }
        return content.length() > 50 ? content.substring(0, 50) + "..." : content;
    }
    
    @Override
    public String getStatusDescription() {
        if (status == null) {
            return "未知";
        }
        return switch (status) {
            case 0 -> "失败";
            case 1 -> "成功";
            case 2 -> "处理中";
            default -> "未知";
        };
    }
    
    /**
     * 获取角色显示名称
     */
    public String getRoleDisplayName() {
        if (role == null) {
            return "未知";
        }
        return switch (role) {
            case "user" -> "用户";
            case "assistant" -> "助手";
            case "system" -> "系统";
            default -> role;
        };
    }
    
    /**
     * 获取消息类型显示名称
     */
    public String getMessageTypeDisplayName() {
        if (messageType == null) {
            return "文本";
        }
        return switch (messageType) {
            case "text" -> "文本";
            case "image" -> "图片";
            case "file" -> "文件";
            default -> messageType;
        };
    }
    
    /**
     * 获取格式化的创建时间
     */
    public String getFormattedCreateTime() {
        if (getCreateTime() == null) {
            return "";
        }
        
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime createTime = getCreateTime();
        
        // 如果是今天，只显示时间
        if (createTime.toLocalDate().equals(now.toLocalDate())) {
            return createTime.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
        }
        
        // 如果是昨天，显示"昨天 HH:mm"
        if (createTime.toLocalDate().equals(now.toLocalDate().minusDays(1))) {
            return "昨天 " + createTime.format(DateTimeFormatter.ofPattern("HH:mm"));
        }
        
        // 其他情况显示完整日期时间
        return createTime.format(DateTimeFormatter.ofPattern("MM-dd HH:mm"));
    }
    
    /**
     * 获取格式化的token数量
     */
    public String getFormattedTokenCount() {
        if (tokenCount == null || tokenCount == 0) {
            return "0";
        }
        if (tokenCount >= 1000) {
            return String.format("%.1fK", tokenCount / 1000.0);
        }
        return tokenCount.toString();
    }
    
    /**
     * 是否有附件
     */
    public boolean hasAttachments() {
        return attachments != null && !attachments.isEmpty();
    }
    
    /**
     * 是否是用户消息
     */
    public boolean isUserMessage() {
        return "user".equals(role);
    }
    
    /**
     * 是否是助手消息
     */
    public boolean isAssistantMessage() {
        return "assistant".equals(role);
    }
}
